import Singleton from "./Singleton";
import MsgHints from "../framwork/MsgHints";
import Utils from '../utils/Utils';
const wx = window["wx"]

const { ccclass, property } = cc._decorator;
export default class AdCenter extends Singleton {
    VideoAd = null;
    gridAd = null;

    constructor() {
        super();
        if (cc.sys.platform == cc.sys.WECHAT_GAME && wx.createRewardedVideoAd) {
            this.VideoAd = wx.createRewardedVideoAd({
                adUnitId: 'adunit-82d2ffa7c533c4b9'
            });

            this.VideoAd.onError((res) => {
                console.log("onError", res);
                MsgHints.show("广告加载错误");
            });

            this.VideoAd.onLoad(() => {
                // console.log('广告加载成功');
            })

            this.VideoAd.onClose(res => {
                if (res && res.isEnded || res === undefined) {
                    cc.log("正常播放结束，可以下发游戏奖励")
                    this.callBack(true);
                }
                else {
                    cc.log("播放中途退出，不下发游戏奖励")
                    this.callBack(false);
                }
            })

            // 创建插屏广告实例，提前初始化
            if (wx.createInterstitialAd) {
                this.interstitialAd = wx.createInterstitialAd({
                    adUnitId: 'adunit-1ecf6f62a4773167'
                })

                this.interstitialAd.onError((res) => {
                    console.log("插屏onError", res);
                });
            }

            // 创建格子广告实例，提前初始化
            let { screenWidth, screenHeight } = wx.getSystemInfoSync()
            this.gridAd = wx.createGridAd({
                adUnitId: 'adunit-8a1c5d8db2462f5d',
                adTheme: 'black',
                gridCount: 5,
                style: {
                    left: 0,
                    top: screenHeight - 150,
                    width: screenWidth,
                    opacity: 0.8
                }
            })

            this.gridAd.onError((res) => {
                console.log("格子onError", res);
            });
        }
    }

    interstitialAd = null
    public showinterstitialAd() {
        // 在适合的场景显示插屏广告
        if (this.interstitialAd) {
            this.interstitialAd.show().catch((err) => {
                console.log(err);
            })
        }
    }

    public showGridAd() // 在适合的场景显示格子广告
    {
        if (this.gridAd)
            this.gridAd.show()
    }

    public hideGridAd() {
        if (this.gridAd)
            this.gridAd.hide()
    }


    private callBack: Function;
    private _lasttryplaytime: number = 0;

    //type 暂时没有用
    public play(type: number, callback: Function) {
        if (Utils.getServerTime() - this._lasttryplaytime < 1000) {
            console.log("点击过于频繁")
            return;
        }
        console.log("尝试播放广告")
        this._lasttryplaytime = Utils.getServerTime();
        this.callBack = callback;

        if (cc.sys.platform == cc.sys.WECHAT_GAME && this.VideoAd) {
            // 用户触发广告后，显示激励视频广告
            this.VideoAd.show().catch(() => {
                // 失败重试
                this.VideoAd.load()
                    .then(() => this.VideoAd.show())
                    .catch(err => {
                        cc.log('激励视频 广告显示失败');
                        // MsgHints.show("暂时没有广告" + err.errCode + "/" + err.errMsg);
                        this.callBack(false);
                    })
            })
        }
        else {
            this.callBack(true);
        }
    }
}